<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Resource Autoloading - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/learning.autoloading.resources.html">Inglês (English)</a></li>
    <li><a href="../pt-br/learning.autoloading.resources.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="learning.autoloading.usage.html">Basic Autoloader Usage</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="learning.autoloading.html">Auto-carregamento no Zend Framework</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="learning.autoloading.conclusion.html">Conclusion</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="learning.autoloading.resources" class="section"><div class="info"><h1 class="title">Resource Autoloading</h1></div>
    

    <p class="para">
        Often, when developing an application, it&#039;s either difficult to package classes in the 1:1
        classname:filename standard Zend Framework recommends, or it&#039;s advantageous for purposes of
        packaging not to do so. However, this means you class files will not be found by the
        autoloader.
    </p>

    <p class="para">
        If you read through <a href="learning.autoloading.design.html" class="link">the design goals</a> for
        the autoloader, the last point in that section indicated that the solution should cover this
        situation. Zend Framework does so with
        <span class="classname">Zend_Loader_Autoloader_Resource</span>.
    </p>

    <p class="para">
        A resource is just a name that corresponds to a component namespace (which
        is appended to the autoloader&#039;s namespace) and a path (which is relative to
        the autoloader&#039;s base path). In action, you&#039;d do something like this:
    </p>

    <pre class="programlisting brush: php">
$loader = new Zend_Application_Module_Autoloader(array(
    &#039;namespace&#039; =&gt; &#039;Blog&#039;,
    &#039;basePath&#039;  =&gt; APPLICATION_PATH . &#039;/modules/blog&#039;,
));
</pre>


    <p class="para">
        Once you have the loader in place, you then need to inform it of the various resource types
        it&#039;s aware of. These resource types are simply pairs of subtree and prefix.
    </p>

    <p class="para">
        As an example, consider the following tree:
    </p>

    <pre class="programlisting brush: text">
path/to/some/resources/
|-- forms/
|   `-- Guestbook.php        // Foo_Form_Guestbook
|-- models/
|   |-- DbTable/
|   |   `-- Guestbook.php    // Foo_Model_DbTable_Guestbook
|   |-- Guestbook.php        // Foo_Model_Guestbook
|   `-- GuestbookMapper.php  // Foo_Model_GuestbookMapper
</pre>


    <p class="para">
        Our first step is creating the resource loader:
    </p>

    <pre class="programlisting brush: php">
$loader = new Zend_Loader_Autoloader_Resource(array(
    &#039;basePath&#039;  =&gt; &#039;path/to/some/resources/&#039;,
    &#039;namespace&#039; =&gt; &#039;Foo&#039;,
));
</pre>


    <p class="para">
        Next, we need to define some resource types.
         <span class="methodname">Zend_Loader_Autoloader_Resourse::addResourceType()</span> has three
        arguments: the &quot;type&quot; of resource (an arbitrary string), the path under the base path in
        which the resource type may be found, and the component prefix to use for the resource type.
        In the above tree, we have three resource types: form (in the subdirectory &quot;forms&quot;, with a
        component prefix of &quot;Form&quot;), model (in the subdirectory &quot;models&quot;, with a component prefix of
        &quot;Model&quot;), and dbtable (in the subdirectory &quot;<var class="filename">models/DbTable</var>&quot;,
        with a component prefix of &quot;<span class="classname">Model_DbTable</span>&quot;). We&#039;d define them as
        follows:
    </p>

    <pre class="programlisting brush: php">
$loader-&gt;addResourceType(&#039;form&#039;, &#039;forms&#039;, &#039;Form&#039;)
       -&gt;addResourceType(&#039;model&#039;, &#039;models&#039;, &#039;Model&#039;)
       -&gt;addResourceType(&#039;dbtable&#039;, &#039;models/DbTable&#039;, &#039;Model_DbTable&#039;);
</pre>


    <p class="para">
        Once defined, we can simply use these classes:
    </p>

    <pre class="programlisting brush: php">
$form      = new Foo_Form_Guestbook();
$guestbook = new Foo_Model_Guestbook();
</pre>


    <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>Module Resource Autoloading</b><br /></span>
        

        <p class="para">
            Zend Framework&#039;s <acronym class="acronym">MVC</acronym> layer encourages the use of &quot;modules&quot;, which
            are self-contained applications within your site. Modules typically have a number of
            resource types by default, and Zend Framework even
            <a href="project-structure.filesystem.html" class="link">recommends a standard directory layout
                for modules</a>. Resource autoloaders are therefore
            quite useful in this paradigm -- so useful that they are enabled by default when you
            create a bootstrap class for your module that extends
            <span class="classname">Zend_Application_Module_Bootstrap</span>. For more information, read
            the <a href="zend.loader.autoloader-resource.html#zend.loader.autoloader-resource.module" class="link">Zend_Loader_Autoloader_Module
                documentation</a>.
        </p>
    </p></blockquote>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="learning.autoloading.usage.html">Basic Autoloader Usage</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="learning.autoloading.html">Auto-carregamento no Zend Framework</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="learning.autoloading.conclusion.html">Conclusion</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="learning.html">Conhecendo o Zend Framework</a></li>
  <li class="header up"><a href="learning.autoloading.html">Auto-carregamento no Zend Framework</a></li>
  <li><a href="learning.autoloading.intro.html">Introdu&ccedil;&atilde;o</a></li>
  <li><a href="learning.autoloading.design.html">Goals and Design</a></li>
  <li><a href="learning.autoloading.usage.html">Basic Autoloader Usage</a></li>
  <li class="active"><a href="learning.autoloading.resources.html">Resource Autoloading</a></li>
  <li><a href="learning.autoloading.conclusion.html">Conclusion</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>